import express, {Express, Router, Request, Response} from 'express'
const axios = require('axios')
const app:Express = express()
const router:Router =express.Router()

app.use('/api', router)
router.use('*', (req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*')
    next()
})
// req前端请求信息 res后端响应信息
router.get('/list', async (req: Request, res: Response) => {
    // console.log('req', req);
    const result = await axios.post('xxx')
    console.log('result',result);
    
    res.json({
        data: {
            name: 'xiaowu',
            age: 18
        },
        msg: '请求成功'
    })
    
})
// 延时请求
router.get('/delay', (req, res) => {
    let query = req.query
    setTimeout(() => {
        res.send('请求成功，请求延时==' + query.delay)
    }, Number(query.delay) || 0)
})

// 起服务
app.listen('3333', () => {
    console.log('express server is runing at 3333');
    
})